﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using Dracham.MapEditor.Singleton;
using System.IO;

namespace Dracham.MapEditor
{
    public partial class frmMapCompiler : Form
    {
        private Thread workerThread;

        public frmMapCompiler()
        {
            InitializeComponent();

            workerThread = new Thread(BuildMaps);
            workerThread.Start();
        }

        #region cross-thread-updaters 

        private delegate void UpdateStatusTextCallback(String text);
        private void UpdateStatusText(String text)
        {
            if (this.lblCurrentAction.InvokeRequired)
            {
                UpdateStatusTextCallback cb = new UpdateStatusTextCallback(UpdateStatusText);
                this.Invoke(cb, text);
            }
            else
            {
                lstActions.Items.Insert(0, lblCurrentAction.Text);
                lblCurrentAction.Text = text;
            }
        }

        private delegate void TickProgressCallback(Int32 max);
        private void TickProgress(Int32 max)
        {
            if (this.pbProgress.InvokeRequired)
            {
                TickProgressCallback cb = new TickProgressCallback(TickProgress);
                this.Invoke(cb, max);
            }
            else
            {
                this.pbProgress.Maximum = max;
                this.pbProgress.Value++;
            }
        }

        #endregion

        private void BuildMaps()
        {
            //
            // todo: logic for building the maps...
            //

            XmlDocument xz = DrachamMapEditor.ZoneManager.GetXmlDocument();
            XmlDocument xt = DrachamMapEditor.TextureManager.GetXmlDocument();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<ZoneData>");

            UpdateStatusText("Getting zones");
            sb.AppendLine(xz.GetElementsByTagName("Zones")[0].OuterXml);

            UpdateStatusText("Getting textures");
            sb.AppendLine(xt.GetElementsByTagName("Textures")[0].OuterXml);

            sb.AppendLine("</ZoneData>");

            UpdateStatusText("Save file");
            this.Invoke((MethodInvoker)delegate()
            {
                if (sfdSaveZoneDataFile.ShowDialog() == DialogResult.OK)
                {
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(sb.ToString());

                    xdoc.Save(sfdSaveZoneDataFile.FileName);
                }

                btnOkey.Enabled = true;
            });
        }

        private void btnOkey_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
